﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CCWin;
using BookModel;

namespace WinFromBook
{
    public partial class FrmBookInfo : CCSkinMain
    {
        public FrmBookInfo()
        {
            InitializeComponent();
        }
        #region 封装方法，字段，执行操作
        int PageNum = 1;//当前页
        int PageCount = 10;//每页显示
        int PageTotal;//总页数
        List<BookInfo> Lb = new BLL.BookInfoBLL().GetBookInfo();
        public void BookInfoShow(List<BookInfo> Lb3)//分页加载图书信息
        {
            // List<BookInfo> Lb = new BLL.BookInfoBLL().GetBookInfo();
            if ((Lb3.Count % PageCount) == 0)
                PageTotal = Lb3.Count / PageCount;
            else
                PageTotal = (Lb3.Count / PageCount) + 1;
            var BookInfo = Lb3.Skip(PageCount * (PageNum - 1)).Take(PageCount);
            this.SDGV.DataSource = BookInfo.ToList();
            this.linkthis.Text = "当前页：" + PageNum + "/" + PageTotal + "页";
        }
        //加载TreeView
        public void typeShow()
        {
            this.LV.Items.Clear();
            foreach (var item in new BLL.TypeInfoBLL().GetTYpeInfo())
            {
                ListViewItem lvi = new ListViewItem(item.TName);
                this.LV.Items.Add(lvi);
            }
        }
        //首页
        public void PageTotalone()
        {
            if (PageNum == 1)
            {
                MessageBox.Show("已经是首页了", "系统提示");
                return;
            }
            PageNum = 1;
            BookInfoShow(Lb);
        }
        //上一页
        public void PageTotalCut()
        {
            if (PageNum == 1)
            {
                MessageBox.Show("已经是首页了", "系统提示");
                return;
            }
            PageNum--;
            BookInfoShow(Lb);
        }
        //下一页
        public void PageTotalAdd()
        {
            if (PageNum == PageTotal)
            {
                MessageBox.Show("已经是尾页了", "系统提示");
                return;
            }
            PageNum++;
            BookInfoShow(Lb);
        }
        //尾页
        public void PageTotalLast()
        {
            if (PageNum == PageTotal)
            {
                MessageBox.Show("已经是尾页了", "系统提示");
                return;
            }
            PageNum = PageTotal;
            BookInfoShow(Lb);
        }
        //图书更新
        public void UpdateBook()
        {
            FrmBookInsert FbI = new FrmBookInsert();
            FbI.Text = "图书更新";
            FbI.Btype = this.SDGV.SelectedRows[0].Cells["Tname"].Value.ToString();
            FbI.Bname = this.SDGV.SelectedRows[0].Cells[1].Value.ToString();
            FbI.BRNaem = this.SDGV.SelectedRows[0].Cells["Rname"].Value.ToString();
            FbI.fbi = this;
            FbI.Show();
        }
        //点击图书类别
        public void ClickType()
        {
            List<BookInfo> Lb2 = new List<BookInfo>();
            string Tname = this.LV.SelectedItems[0].SubItems[0].Text;
            foreach (var item in new BLL.BookInfoBLL().GetBookInfo())
            {
                if (item.TName == Tname)
                {
                    Lb2.Add(item);
                }
            }
            PageNum = 1;
            BookInfoShow(Lb2);
        }
        #endregion

        private void linkTop_Click(object sender, EventArgs e)
        {
            PageTotalone();
        }

        private void FrmBookInfo_Load(object sender, EventArgs e)
        {
            BookInfoShow(Lb);
            typeShow();
        }

        private void linkPageS_Click(object sender, EventArgs e)
        {
            PageTotalCut();
        }

        private void linkPageX_Click(object sender, EventArgs e)
        {
            PageTotalAdd();
        }

        private void linkLast_Click(object sender, EventArgs e)
        {
            PageTotalLast();
        }

        private void LV_Click(object sender, EventArgs e)
        {
            ClickType();
        }

        private void LV_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            BookInfoShow(Lb);
        }

        private void skinButtonAdd_Click(object sender, EventArgs e)
        {
            FrmBookInsert fb = new FrmBookInsert();
            fb.fbi = this;
            fb.Show();
        }

        private void 更新图书信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UpdateBook();
        }
    }
}
